package com.app.couponsharelife.ui.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.RemoteDataSource

/**
 * Created by guluwa on 2018/12/5.
 */
class TribeViewModel : BaseViewModel() {

    private var userSignFresh = MutableLiveData<FreshBean>()

    private var userSignResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun userSign(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (userSignResult == null) {
            userSignResult = Transformations.switchMap(userSignFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().userSign(it.map)
                } else {
                    null
                }
            }
        }
        return userSignResult!!
    }

    fun refreshUserSign(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) userSignFresh.value = FreshBean(map, isFresh)
    }

    private var getUserSignInfoFresh = MutableLiveData<FreshBean>()

    private var getUserSignInfoResult: LiveData<ViewDataBean<RemoteResultBean<SignInfoBean>>>? = null

    fun getUserSignInfo(): LiveData<ViewDataBean<RemoteResultBean<SignInfoBean>>>? {
        if (getUserSignInfoResult == null) {
            getUserSignInfoResult = Transformations.switchMap(getUserSignInfoFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getUserSignInfo(it.map)
                } else {
                    null
                }
            }
        }
        return getUserSignInfoResult!!
    }

    fun refreshGetUserSignInfo(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getUserSignInfoFresh.value = FreshBean(map, isFresh)
    }

    private var getCouponKingFresh = MutableLiveData<FreshBean>()

    private var getCouponKingResult: LiveData<ViewDataBean<RemoteResultBean<CouponMoneyBean>>>? = null

    fun getCouponKing(): LiveData<ViewDataBean<RemoteResultBean<CouponMoneyBean>>>? {
        if (getCouponKingResult == null) {
            getCouponKingResult = Transformations.switchMap(getCouponKingFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getCouponKing(it.map)
                } else {
                    null
                }
            }
        }
        return getCouponKingResult!!
    }

    fun refreshGetCouponKing(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getCouponKingFresh.value = FreshBean(map, isFresh)
    }

    private var addCouponKingFresh = MutableLiveData<FreshBean>()

    private var addCouponKingResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun addCouponKing(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (addCouponKingResult == null) {
            addCouponKingResult = Transformations.switchMap(addCouponKingFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().addCouponKing(it.map)
                } else {
                    null
                }
            }
        }
        return addCouponKingResult!!
    }

    fun refreshAddCouponKing(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) addCouponKingFresh.value = FreshBean(map, isFresh)
    }

    private var getCouponKingRecordPageFresh = MutableLiveData<FreshBean>()

    private var getCouponKingRecordPageResult: LiveData<ViewDataBean<RemoteResultBean<CouponMoneyRecordBean>>>? = null

    fun getCouponKingRecordPage(): LiveData<ViewDataBean<RemoteResultBean<CouponMoneyRecordBean>>>? {
        if (getCouponKingRecordPageResult == null) {
            getCouponKingRecordPageResult = Transformations.switchMap(getCouponKingRecordPageFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getCouponKingRecordPage(it.map)
                } else {
                    null
                }
            }
        }
        return getCouponKingRecordPageResult!!
    }

    fun refreshGetCouponKingRecordPage(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getCouponKingRecordPageFresh.value = FreshBean(map, isFresh)
    }

    private var getCouponForceRecordPageFresh = MutableLiveData<FreshBean>()

    private var getCouponForceRecordPageResult: LiveData<ViewDataBean<RemoteResultBean<CouponMoneyRecordBean>>>? = null

    fun getCouponForceRecordPage(): LiveData<ViewDataBean<RemoteResultBean<CouponMoneyRecordBean>>>? {
        if (getCouponForceRecordPageResult == null) {
            getCouponForceRecordPageResult = Transformations.switchMap(getCouponForceRecordPageFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getCouponForceRecordPage(it.map)
                } else {
                    null
                }
            }
        }
        return getCouponForceRecordPageResult!!
    }

    fun refreshGetCouponForceRecordPage(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getCouponForceRecordPageFresh.value = FreshBean(map, isFresh)
    }
}